library(WDI)
library(sf)
library(tmap)
library(rnaturalearth)
library(rnaturalearthdata)
library(tidyverse)
library(tidycensus)
library(raster)
library(exactextractr)
library(terra)
## Warning: package 'DT' was built under R version 4.0.5
## tmap mode set to plotting
In the course of working on a GIS project, you may have to perform certain tasks multiple times. For example, you may want to implement a spatial join (a procedure we learned about last class) between a point layer and several different polygon datasets that reflect different geographic scales. Or, you may want to quickly make multiple maps, to get a sense of how different variables are distributed across space before beginning a more in-depth study.
Dedicated GIS applications like ArcGIS and QGIS have built-in scripting windows that allow users to write Python scripts that can automate repetitive procedures. In addition, for users unfamiliar with Python, ArcGIS provides tools like Model Builder, and embeds batch-processing functions embedded in its menu-bused interface.
In this lesson, we’ll become familiar with some basic scripting tools that can help you to automate some of the tasks we have already covered in the course with a minimal amount of programming. These tools will come in handy if you decide to work with GIS in a longer-term project and you want to save time on your geoprocessing tasks.
Before diving into some GIS-specific examples, let’s become familiar with some basic concepts and processes using a simple example. Let’s say you have a large collection of temperature data, measured in Fahrenheit, and you want to convert these data to Celsius. Recall that the formula to convert between Fahrenheit and Celsius is as follows:
# fahrenheit to Celsius formula, where "F" is fahrenheit input
(F-32)*(5/9)
## [1] -17.77778
At its most basic level, R is a calculator; if for example, one of our Fahrenheit measurements is 55 degrees; we can convert this to Celsius by plugging 55 into the conversion formula:
# Converts 55 degrees fahrenheit to Celsius
(55-32)*(5/9)
## [1] 12.77778
This is easy enough, but if we have a large amount of temperature data that requires processing, we wouldn’t want to carry out this calculation for each measurement in our data collection. The first step in allowing us to carry out this conversion operation at scale is to write a function, which is simply a programming construct that takes a set of inputs (also known as arguments), manipulates those inputs/arguments in a specific way (the body of the function), and returns an output that is the product of how those inputs are manipulated in the body of the function. It is much like a recipe, where the recipe’s ingredients are analogous to a function’s inputs, the instructions about how to combine and process those ingredients are analogous to the body of the function, and the end product of the recipe (for example, a cake) is analogous to the function’s output.
Let’s see how we can wrap the Fahrenheit-Celsius formula above into a function:
fahrenheit_to_celsius_converter<-function(fahrenheit_input){
celsius_output<-(fahrenheit_input-32)*(5/9)
return(celsius_output)
}
Let’s unpack the code above, which we used to create our function:
function; within the parenthesis after function, we specify the function’s argument(s). Here, the function’s argument is an input named fahrenheit_input. The name of the argument(s) is arbitrary, and can be anything you like; ideally, its name should be informed by relevant context. Here, the argument/input to the function is a temperature value expressed in degrees Fahrenheit, so the name “fahrenheit_input” describes the nature of this input.{, and then define the body of the function (i.e. the recipe), which specifies how we want to transform this input. In particular, we take fahrenheit_input, subtract 32, and then multiply by 5/9, which transform the input to the celsius temperature scale. We’ll tell R to assign this transformed value to a new object, named celsius_output.return(celsius_output), we specify the value we want the function to return. Here, we are saying that we want the function to return the value that was assigned to celsius_output. We then close the function by typing a left-facing curly brace below the return statement }.fahrenheit_to_celsius_converter.After running that code, we can use the newly created fahrenheit_to_celsius function to perform our Fahrenheit to Celsius transformations. Let’s say we have a Fahrenheit value of 68, and want to transform it to Celsius:
fahrenheit_to_celsius_converter(fahrenheit_input=68)
## [1] 20
Above, we passed the argument “fahrenheit_input=68” to the fahrenheit_to_celsius_converter function that we created; the function then took this value (68), plugged it into “fahrenheit_input” within the function and assigned the resulting value to “celsius_output”; it then returned the value of “celsius_output” (20) back to us.
Let’s try another one:
fahrenheit_to_celsius_converter(fahrenheit_input=22)
## [1] -5.555556
In short, we can specify any value for the “fahrenheit_input” argument; this value will be substituted for “fahrenheit_input” in the expression celsius_output<-(fahrenheit_input-32)*(5/9), after which the value of celsius_output will be returned to us.
Using this newly created function helps us to avoid manually converting each of our temperature values from the Fahrenheit scale to the Celsius scale; instead of repeating the calculation over and over manually, we could simply plug our Fahrenheit temperature values into the function, and let the function carry out the calculation for us. However, it is still time-consuming to plug our Fahrenheit values into the function one-by-one. For example, let’s say we have a vector of Fahrenheit temperature values; below, we’ll assign these values to an object named fahrenheit_input_vector:
fahrenheit_input_vector<-c(45.6, 95.9, 67.8, 43)
If we wanted to convert all of these Fahrenheit values to the Celsius scale, we could do so individually, i.e.
fahrenheit_to_celsius_converter(fahrenheit_input=45.6)
## [1] 7.555556
And so on.
However, we can also iteratively apply our function to all of these vector elements, and deposit the transformed results into a new vector; we’ll assign this new vector to an object named celsius_outputs_vector:
celsius_outputs_vector<-map_dbl(fahrenheit_input_vector, fahrenheit_to_celsius_converter)
The code above passes two elements to the map_dbl() function, which is a function that can be used to iteratively pass multiple arguments to a function, and return the results as a numeric vector. In the case above, we take fahrenheit_input_vector (i.e. a vector with the numbers 45.6, 95.9, 67.8, 43), and run each of these numbers through the fahrenheit_converter() function, and sequentially deposit the transformed result to the newly created celsius_outputs_vector() object, which looks as follows:
celsius_outputs_vector
## [1] 7.555556 35.500000 19.888889 6.111111
In short, the code that reads celsius_outputs_vector<-map_dbl(fahrenheit_input_vector, fahrenheit_converter) did the following:
fahrenheit_input_vector) through the fahrenheit_converter() function, and place the output (7.555556) as the first element in a new vector of transformed values, named celsius_outputs_vector.fahrenheit_input_vector) through the fahrenheit_converter() function, and deposit the output (35.500000) as the second element in celsius_outputs_vector.In programming languages, functions are typically applied to multiple inputs in an iterative fashion using a construct known as a for-loop, which some of you may already be familiar with. R users also frequently use specialized functions (instead of for-loops) to iterate over elements; this is often faster, or at the very least, makes R scripts more readable. One family of these iterative functions is the “Apply” family of functions. A more recent set of functions that facilitate iteration is part of the tidyverse, and is found within the purrr package. These functions known as map() functions, and we will use them here to iteratively apply our functions to multiple inputs (the “map” label might be confusing when working in a GIS setting where you might be making actual maps, i.e. spatial visualizations; however, it should be clear from the context whether we are referring to map() functions within purrr, or to actual maps).
celsius_outputs_vector<-map_dbl(fahrenheit_input_vector, fahrenheit_to_celsius_converter)
celsius_outputs_vector
## [1] 7.555556 35.500000 19.888889 6.111111
temperature_conversion_dataset<-cbind(fahrenheit_input_vector, celsius_outputs_vector)
temperature_conversion_dataset
## fahrenheit_input_vector celsius_outputs_vector
## [1,] 45.6 7.555556
## [2,] 95.9 35.500000
## [3,] 67.8 19.888889
## [4,] 43.0 6.111111
celsius_outputs<-map(fahrenheit_input_vector, fahrenheit_to_celsius_converter)
celsius_outputs
## [[1]]
## [1] 7.555556
##
## [[2]]
## [1] 35.5
##
## [[3]]
## [1] 19.88889
##
## [[4]]
## [1] 6.111111
celsius_outputs[[2]]
## [1] 35.5
CO_aurora_policestops<-read_csv("https://www.dropbox.com/s/u7xqa7dc34hlsfp/co_aurora_2020_04_01.csv?dl=1")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## raw_row_number = col_character(),
## date = col_date(format = ""),
## time = col_time(format = ""),
## location = col_character(),
## lat = col_double(),
## lng = col_double(),
## district = col_double(),
## subject_age = col_double(),
## subject_race = col_character(),
## subject_sex = col_character(),
## type = col_character(),
## violation = col_character(),
## citation_issued = col_logical(),
## outcome = col_character(),
## raw_ethnicity = col_character(),
## raw_race = col_character()
## )
# Define spatial object
co_aurora_sf<-CO_aurora_policestops %>%
drop_na(lng) %>%
st_as_sf(coords=c("lng", "lat"), crs=4326)
# Project "co_aurora_sf"
co_aurora_sf_project<-co_aurora_sf %>% st_transform(2232)
CO_tracts<-get_decennial(geography = "tract",
state="CO",
variables = "P001001",
year = 2010,
geometry=TRUE) %>%
st_transform(2232)
# map points
tm_shape(CO_tracts)+
tm_polygons()+
tm_shape(co_aurora_sf_project)+
tm_dots()
# execute join
stops_tracts_join<-st_join(co_aurora_sf_project, CO_tracts)
# extract stops/tract
stop_per_tract<-stops_tracts_join %>%
group_by(GEOID, NAME) %>%
summarize(n())
## `summarise()` has grouped output by 'GEOID'. You can override using the
## `.groups` argument.
stop_per_tract
## Simple feature collection with 148 features and 3 fields
## geometry type: GEOMETRY
## dimension: XY
## bbox: xmin: 3068813 ymin: 1622486 xmax: 3275419 ymax: 1790404
## projected CRS: NAD83 / Colorado Central (ftUS)
## # A tibble: 148 × 4
## # Groups: GEOID [148]
## GEOID NAME `n()` geometry
## <chr> <chr> <int> <GEOMETRY [US_survey_foo>
## 1 08001007801 Census Tract 78.01, Adams County… 3108 MULTIPOINT ((3173039 169…
## 2 08001007802 Census Tract 78.02, Adams County… 1459 MULTIPOINT ((3178342 169…
## 3 08001007900 Census Tract 79, Adams County, C… 1304 MULTIPOINT ((3173036 169…
## 4 08001008000 Census Tract 80, Adams County, C… 1186 MULTIPOINT ((3178335 169…
## 5 08001008100 Census Tract 81, Adams County, C… 1901 MULTIPOINT ((3183605 169…
## 6 08001008200 Census Tract 82, Adams County, C… 3359 MULTIPOINT ((3181408 170…
## 7 08001008308 Census Tract 83.08, Adams County… 895 MULTIPOINT ((3189032 170…
## 8 08001008309 Census Tract 83.09, Adams County… 8803 MULTIPOINT ((3193951 170…
## 9 08001008353 Census Tract 83.53, Adams County… 447 MULTIPOINT ((3204594 169…
## 10 08001008401 Census Tract 84.01, Adams County… 1 POINT (3257658 1706182)
## # … with 138 more rows
stop_per_tract_final<-stop_per_tract %>%
rename(traffic_stops="n()")
traffic_stop_geography<-function(desired_geography){
geography_extract<-get_decennial(geography = desired_geography,
state="CO",
variables = "P001001",
year = 2010,
geometry=TRUE) %>%
st_transform(2232)
stop_geography_join<-st_join(co_aurora_sf_project, geography_extract)
stops_per_geography<-stop_geography_join %>%
group_by(GEOID, NAME) %>%
summarize(n()) %>%
rename(traffic_stops="n()")
stops_per_geography_df<-as.data.frame(stops_per_geography) %>%
dplyr::select(-geometry)
return(stops_per_geography_df)
}
county_subdivision_stops<-traffic_stop_geography(desired_geography="county subdivision")
## Getting data from the 2010 decennial Census
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using Census Summary File 1
##
|
| | 0%
|
|====== | 9%
|
|============ | 17%
|
|============== | 20%
|
|==================== | 29%
|
|======================================================================| 100%
## `summarise()` has grouped output by 'GEOID'. You can override using the
## `.groups` argument.
desired_geography_inputs<-c("county", "tract", "zcta")
geography_stop_list<-map(desired_geography_inputs, traffic_stop_geography)
## Getting data from the 2010 decennial Census
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using Census Summary File 1
##
|
| | 0%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|==== | 5%
|
|===== | 7%
|
|====== | 9%
|
|======= | 10%
|
|======= | 11%
|
|======== | 12%
|
|========= | 13%
|
|========== | 14%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============= | 18%
|
|============= | 19%
|
|============== | 20%
|
|=============== | 21%
|
|================ | 22%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 29%
|
|===================== | 30%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|===================================== | 54%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 66%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================= | 71%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 83%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|=============================================================== | 89%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 98%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 100%
## `summarise()` has grouped output by 'GEOID'. You can override using the
## `.groups` argument.
## Getting data from the 2010 decennial Census
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using Census Summary File 1
## `summarise()` has grouped output by 'GEOID'. You can override using the `.groups` argument.
## Getting data from the 2010 decennial Census
##
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
##
## Using Census Summary File 1
##
|
| | 0%
|
|= | 2%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|====== | 8%
|
|====== | 9%
|
|======= | 10%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 19%
|
|================ | 23%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================== | 44%
|
|=============================== | 44%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 46%
|
|================================= | 47%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|=================================== | 51%
|
|==================================== | 51%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================== | 65%
|
|=============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 69%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================== | 80%
|
|========================================================= | 81%
|
|========================================================== | 83%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 90%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================= | 94%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|==================================================================== | 98%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
## `summarise()` has grouped output by 'GEOID'. You can override using the
## `.groups` argument.
geography_stop_list[[1]]
## GEOID NAME traffic_stops
## 1 08001 Adams County, Colorado 22489
## 2 08005 Arapahoe County, Colorado 117391
## 3 08013 Boulder County, Colorado 1
## 4 08031 Denver County, Colorado 1244
## 5 08035 Douglas County, Colorado 383
traffic_stops_tract<-geography_stop_list[[2]]
traffic_stops_tract
## GEOID NAME traffic_stops
## 1 08001007801 Census Tract 78.01, Adams County, Colorado 3108
## 2 08001007802 Census Tract 78.02, Adams County, Colorado 1459
## 3 08001007900 Census Tract 79, Adams County, Colorado 1304
## 4 08001008000 Census Tract 80, Adams County, Colorado 1186
## 5 08001008100 Census Tract 81, Adams County, Colorado 1901
## 6 08001008200 Census Tract 82, Adams County, Colorado 3359
## 7 08001008308 Census Tract 83.08, Adams County, Colorado 895
## 8 08001008309 Census Tract 83.09, Adams County, Colorado 8803
## 9 08001008353 Census Tract 83.53, Adams County, Colorado 447
## 10 08001008401 Census Tract 84.01, Adams County, Colorado 1
## 11 08001008523 Census Tract 85.23, Adams County, Colorado 1
## 12 08001988700 Census Tract 9887, Adams County, Colorado 25
## 13 08005005612 Census Tract 56.12, Arapahoe County, Colorado 1
## 14 08005005628 Census Tract 56.28, Arapahoe County, Colorado 1
## 15 08005005800 Census Tract 58, Arapahoe County, Colorado 1
## 16 08005005951 Census Tract 59.51, Arapahoe County, Colorado 1
## 17 08005006711 Census Tract 67.11, Arapahoe County, Colorado 1
## 18 08005006712 Census Tract 67.12, Arapahoe County, Colorado 2
## 19 08005006854 Census Tract 68.54, Arapahoe County, Colorado 121
## 20 08005006855 Census Tract 68.55, Arapahoe County, Colorado 14
## 21 08005006856 Census Tract 68.56, Arapahoe County, Colorado 122
## 22 08005007104 Census Tract 71.04, Arapahoe County, Colorado 7166
## 23 08005007105 Census Tract 71.05, Arapahoe County, Colorado 698
## 24 08005007106 Census Tract 71.06, Arapahoe County, Colorado 272
## 25 08005007107 Census Tract 71.07, Arapahoe County, Colorado 952
## 26 08005007201 Census Tract 72.01, Arapahoe County, Colorado 2195
## 27 08005007202 Census Tract 72.02, Arapahoe County, Colorado 1168
## 28 08005007301 Census Tract 73.01, Arapahoe County, Colorado 1316
## 29 08005007302 Census Tract 73.02, Arapahoe County, Colorado 5446
## 30 08005007400 Census Tract 74, Arapahoe County, Colorado 1293
## 31 08005007500 Census Tract 75, Arapahoe County, Colorado 510
## 32 08005007600 Census Tract 76, Arapahoe County, Colorado 1038
## 33 08005007702 Census Tract 77.02, Arapahoe County, Colorado 1922
## 34 08005007703 Census Tract 77.03, Arapahoe County, Colorado 1002
## 35 08005007704 Census Tract 77.04, Arapahoe County, Colorado 307
## 36 08005080000 Census Tract 800, Arapahoe County, Colorado 1572
## 37 08005080100 Census Tract 801, Arapahoe County, Colorado 1324
## 38 08005080200 Census Tract 802, Arapahoe County, Colorado 2542
## 39 08005080300 Census Tract 803, Arapahoe County, Colorado 1410
## 40 08005080400 Census Tract 804, Arapahoe County, Colorado 1111
## 41 08005080500 Census Tract 805, Arapahoe County, Colorado 1018
## 42 08005080600 Census Tract 806, Arapahoe County, Colorado 401
## 43 08005080700 Census Tract 807, Arapahoe County, Colorado 943
## 44 08005080800 Census Tract 808, Arapahoe County, Colorado 734
## 45 08005080900 Census Tract 809, Arapahoe County, Colorado 3249
## 46 08005081000 Census Tract 810, Arapahoe County, Colorado 4286
## 47 08005081100 Census Tract 811, Arapahoe County, Colorado 3895
## 48 08005081200 Census Tract 812, Arapahoe County, Colorado 598
## 49 08005081300 Census Tract 813, Arapahoe County, Colorado 3569
## 50 08005081400 Census Tract 814, Arapahoe County, Colorado 2323
## 51 08005081500 Census Tract 815, Arapahoe County, Colorado 3189
## 52 08005081600 Census Tract 816, Arapahoe County, Colorado 2084
## 53 08005081700 Census Tract 817, Arapahoe County, Colorado 389
## 54 08005081800 Census Tract 818, Arapahoe County, Colorado 5590
## 55 08005081900 Census Tract 819, Arapahoe County, Colorado 3641
## 56 08005082000 Census Tract 820, Arapahoe County, Colorado 3252
## 57 08005082100 Census Tract 821, Arapahoe County, Colorado 2796
## 58 08005082200 Census Tract 822, Arapahoe County, Colorado 2216
## 59 08005082300 Census Tract 823, Arapahoe County, Colorado 1511
## 60 08005082400 Census Tract 824, Arapahoe County, Colorado 2295
## 61 08005082500 Census Tract 825, Arapahoe County, Colorado 225
## 62 08005082600 Census Tract 826, Arapahoe County, Colorado 2786
## 63 08005082700 Census Tract 827, Arapahoe County, Colorado 698
## 64 08005082800 Census Tract 828, Arapahoe County, Colorado 1604
## 65 08005082900 Census Tract 829, Arapahoe County, Colorado 3011
## 66 08005083000 Census Tract 830, Arapahoe County, Colorado 211
## 67 08005083100 Census Tract 831, Arapahoe County, Colorado 1409
## 68 08005083200 Census Tract 832, Arapahoe County, Colorado 146
## 69 08005083300 Census Tract 833, Arapahoe County, Colorado 843
## 70 08005083400 Census Tract 834, Arapahoe County, Colorado 455
## 71 08005083500 Census Tract 835, Arapahoe County, Colorado 703
## 72 08005083600 Census Tract 836, Arapahoe County, Colorado 6238
## 73 08005083700 Census Tract 837, Arapahoe County, Colorado 134
## 74 08005083800 Census Tract 838, Arapahoe County, Colorado 2676
## 75 08005083900 Census Tract 839, Arapahoe County, Colorado 3919
## 76 08005084000 Census Tract 840, Arapahoe County, Colorado 150
## 77 08005084100 Census Tract 841, Arapahoe County, Colorado 1432
## 78 08005084200 Census Tract 842, Arapahoe County, Colorado 627
## 79 08005084300 Census Tract 843, Arapahoe County, Colorado 334
## 80 08005084400 Census Tract 844, Arapahoe County, Colorado 154
## 81 08005084500 Census Tract 845, Arapahoe County, Colorado 262
## 82 08005084600 Census Tract 846, Arapahoe County, Colorado 1375
## 83 08005084700 Census Tract 847, Arapahoe County, Colorado 1904
## 84 08005084800 Census Tract 848, Arapahoe County, Colorado 30
## 85 08005084900 Census Tract 849, Arapahoe County, Colorado 90
## 86 08005085000 Census Tract 850, Arapahoe County, Colorado 114
## 87 08005085100 Census Tract 851, Arapahoe County, Colorado 7
## 88 08005085200 Census Tract 852, Arapahoe County, Colorado 86
## 89 08005085300 Census Tract 853, Arapahoe County, Colorado 453
## 90 08005085400 Census Tract 854, Arapahoe County, Colorado 8
## 91 08005085500 Census Tract 855, Arapahoe County, Colorado 17
## 92 08005085600 Census Tract 856, Arapahoe County, Colorado 6
## 93 08005085700 Census Tract 857, Arapahoe County, Colorado 671
## 94 08005085800 Census Tract 858, Arapahoe County, Colorado 512
## 95 08005085900 Census Tract 859, Arapahoe County, Colorado 205
## 96 08005086000 Census Tract 860, Arapahoe County, Colorado 29
## 97 08005086100 Census Tract 861, Arapahoe County, Colorado 12
## 98 08005086300 Census Tract 863, Arapahoe County, Colorado 191
## 99 08005086400 Census Tract 864, Arapahoe County, Colorado 21
## 100 08005086500 Census Tract 865, Arapahoe County, Colorado 1074
## 101 08005086600 Census Tract 866, Arapahoe County, Colorado 426
## 102 08005086700 Census Tract 867, Arapahoe County, Colorado 174
## 103 08005086800 Census Tract 868, Arapahoe County, Colorado 23
## 104 08005086900 Census Tract 869, Arapahoe County, Colorado 21
## 105 08005087000 Census Tract 870, Arapahoe County, Colorado 900
## 106 08005087100 Census Tract 871, Arapahoe County, Colorado 382
## 107 08005087200 Census Tract 872, Arapahoe County, Colorado 4
## 108 08005087300 Census Tract 873, Arapahoe County, Colorado 1
## 109 08013012607 Census Tract 126.07, Boulder County, Colorado 1
## 110 08031001701 Census Tract 17.01, Denver County, Colorado 3
## 111 08031002403 Census Tract 24.03, Denver County, Colorado 1
## 112 08031002601 Census Tract 26.01, Denver County, Colorado 1
## 113 08031002703 Census Tract 27.03, Denver County, Colorado 3
## 114 08031002801 Census Tract 28.01, Denver County, Colorado 3
## 115 08031002803 Census Tract 28.03, Denver County, Colorado 2
## 116 08031002902 Census Tract 29.02, Denver County, Colorado 1
## 117 08031003003 Census Tract 30.03, Denver County, Colorado 6
## 118 08031003004 Census Tract 30.04, Denver County, Colorado 3
## 119 08031003102 Census Tract 31.02, Denver County, Colorado 3
## 120 08031003201 Census Tract 32.01, Denver County, Colorado 9
## 121 08031003202 Census Tract 32.02, Denver County, Colorado 3
## 122 08031003401 Census Tract 34.01, Denver County, Colorado 8
## 123 08031003402 Census Tract 34.02, Denver County, Colorado 1
## 124 08031004102 Census Tract 41.02, Denver County, Colorado 2
## 125 08031004106 Census Tract 41.06, Denver County, Colorado 195
## 126 08031004107 Census Tract 41.07, Denver County, Colorado 26
## 127 08031004301 Census Tract 43.01, Denver County, Colorado 2
## 128 08031004403 Census Tract 44.03, Denver County, Colorado 35
## 129 08031004404 Census Tract 44.04, Denver County, Colorado 15
## 130 08031004405 Census Tract 44.05, Denver County, Colorado 63
## 131 08031006804 Census Tract 68.04, Denver County, Colorado 1
## 132 08031006811 Census Tract 68.11, Denver County, Colorado 1
## 133 08031006813 Census Tract 68.13, Denver County, Colorado 30
## 134 08031006814 Census Tract 68.14, Denver County, Colorado 6
## 135 08031007006 Census Tract 70.06, Denver County, Colorado 164
## 136 08031007037 Census Tract 70.37, Denver County, Colorado 613
## 137 08031007088 Census Tract 70.88, Denver County, Colorado 1
## 138 08031007089 Census Tract 70.89, Denver County, Colorado 3
## 139 08031008304 Census Tract 83.04, Denver County, Colorado 2
## 140 08031008312 Census Tract 83.12, Denver County, Colorado 15
## 141 08031008388 Census Tract 83.88, Denver County, Colorado 4
## 142 08031008389 Census Tract 83.89, Denver County, Colorado 4
## 143 08031008390 Census Tract 83.90, Denver County, Colorado 4
## 144 08031008391 Census Tract 83.91, Denver County, Colorado 1
## 145 08031980000 Census Tract 9800, Denver County, Colorado 10
## 146 08035013901 Census Tract 139.01, Douglas County, Colorado 381
## 147 08035013904 Census Tract 139.04, Douglas County, Colorado 1
## 148 08035014007 Census Tract 140.07, Douglas County, Colorado 1
geography_stop_list %>% pluck(3)
## GEOID NAME traffic_stops
## 1 0880010 ZCTA5 80010, Colorado 20176
## 2 0880011 ZCTA5 80011, Colorado 36835
## 3 0880012 ZCTA5 80012, Colorado 22681
## 4 0880013 ZCTA5 80013, Colorado 12558
## 5 0880014 ZCTA5 80014, Colorado 16634
## 6 0880015 ZCTA5 80015, Colorado 9762
## 7 0880016 ZCTA5 80016, Colorado 4154
## 8 0880017 ZCTA5 80017, Colorado 15986
## 9 0880018 ZCTA5 80018, Colorado 264
## 10 0880019 ZCTA5 80019, Colorado 175
## 11 0880022 ZCTA5 80022, Colorado 1
## 12 0880045 ZCTA5 80045, Colorado 925
## 13 0880102 ZCTA5 80102, Colorado 1
## 14 0880111 ZCTA5 80111, Colorado 4
## 15 0880112 ZCTA5 80112, Colorado 123
## 16 0880113 ZCTA5 80113, Colorado 2
## 17 0880121 ZCTA5 80121, Colorado 1
## 18 0880122 ZCTA5 80122, Colorado 1
## 19 0880137 ZCTA5 80137, Colorado 2
## 20 0880138 ZCTA5 80138, Colorado 16
## 21 0880202 ZCTA5 80202, Colorado 3
## 22 0880203 ZCTA5 80203, Colorado 2
## 23 0880205 ZCTA5 80205, Colorado 1
## 24 0880206 ZCTA5 80206, Colorado 3
## 25 0880207 ZCTA5 80207, Colorado 2
## 26 0880209 ZCTA5 80209, Colorado 11
## 27 0880210 ZCTA5 80210, Colorado 10
## 28 0880218 ZCTA5 80218, Colorado 17
## 29 0880220 ZCTA5 80220, Colorado 82
## 30 0880230 ZCTA5 80230, Colorado 61
## 31 0880231 ZCTA5 80231, Colorado 44
## 32 0880237 ZCTA5 80237, Colorado 1
## 33 0880238 ZCTA5 80238, Colorado 189
## 34 0880239 ZCTA5 80239, Colorado 29
## 35 0880247 ZCTA5 80247, Colorado 721
## 36 0880249 ZCTA5 80249, Colorado 19
## 37 0880303 ZCTA5 80303, Colorado 1
## 38 <NA> <NA> 25
names(geography_stop_list)<-desired_geography_inputs
geography_stop_list
## $county
## GEOID NAME traffic_stops
## 1 08001 Adams County, Colorado 22489
## 2 08005 Arapahoe County, Colorado 117391
## 3 08013 Boulder County, Colorado 1
## 4 08031 Denver County, Colorado 1244
## 5 08035 Douglas County, Colorado 383
##
## $tract
## GEOID NAME traffic_stops
## 1 08001007801 Census Tract 78.01, Adams County, Colorado 3108
## 2 08001007802 Census Tract 78.02, Adams County, Colorado 1459
## 3 08001007900 Census Tract 79, Adams County, Colorado 1304
## 4 08001008000 Census Tract 80, Adams County, Colorado 1186
## 5 08001008100 Census Tract 81, Adams County, Colorado 1901
## 6 08001008200 Census Tract 82, Adams County, Colorado 3359
## 7 08001008308 Census Tract 83.08, Adams County, Colorado 895
## 8 08001008309 Census Tract 83.09, Adams County, Colorado 8803
## 9 08001008353 Census Tract 83.53, Adams County, Colorado 447
## 10 08001008401 Census Tract 84.01, Adams County, Colorado 1
## 11 08001008523 Census Tract 85.23, Adams County, Colorado 1
## 12 08001988700 Census Tract 9887, Adams County, Colorado 25
## 13 08005005612 Census Tract 56.12, Arapahoe County, Colorado 1
## 14 08005005628 Census Tract 56.28, Arapahoe County, Colorado 1
## 15 08005005800 Census Tract 58, Arapahoe County, Colorado 1
## 16 08005005951 Census Tract 59.51, Arapahoe County, Colorado 1
## 17 08005006711 Census Tract 67.11, Arapahoe County, Colorado 1
## 18 08005006712 Census Tract 67.12, Arapahoe County, Colorado 2
## 19 08005006854 Census Tract 68.54, Arapahoe County, Colorado 121
## 20 08005006855 Census Tract 68.55, Arapahoe County, Colorado 14
## 21 08005006856 Census Tract 68.56, Arapahoe County, Colorado 122
## 22 08005007104 Census Tract 71.04, Arapahoe County, Colorado 7166
## 23 08005007105 Census Tract 71.05, Arapahoe County, Colorado 698
## 24 08005007106 Census Tract 71.06, Arapahoe County, Colorado 272
## 25 08005007107 Census Tract 71.07, Arapahoe County, Colorado 952
## 26 08005007201 Census Tract 72.01, Arapahoe County, Colorado 2195
## 27 08005007202 Census Tract 72.02, Arapahoe County, Colorado 1168
## 28 08005007301 Census Tract 73.01, Arapahoe County, Colorado 1316
## 29 08005007302 Census Tract 73.02, Arapahoe County, Colorado 5446
## 30 08005007400 Census Tract 74, Arapahoe County, Colorado 1293
## 31 08005007500 Census Tract 75, Arapahoe County, Colorado 510
## 32 08005007600 Census Tract 76, Arapahoe County, Colorado 1038
## 33 08005007702 Census Tract 77.02, Arapahoe County, Colorado 1922
## 34 08005007703 Census Tract 77.03, Arapahoe County, Colorado 1002
## 35 08005007704 Census Tract 77.04, Arapahoe County, Colorado 307
## 36 08005080000 Census Tract 800, Arapahoe County, Colorado 1572
## 37 08005080100 Census Tract 801, Arapahoe County, Colorado 1324
## 38 08005080200 Census Tract 802, Arapahoe County, Colorado 2542
## 39 08005080300 Census Tract 803, Arapahoe County, Colorado 1410
## 40 08005080400 Census Tract 804, Arapahoe County, Colorado 1111
## 41 08005080500 Census Tract 805, Arapahoe County, Colorado 1018
## 42 08005080600 Census Tract 806, Arapahoe County, Colorado 401
## 43 08005080700 Census Tract 807, Arapahoe County, Colorado 943
## 44 08005080800 Census Tract 808, Arapahoe County, Colorado 734
## 45 08005080900 Census Tract 809, Arapahoe County, Colorado 3249
## 46 08005081000 Census Tract 810, Arapahoe County, Colorado 4286
## 47 08005081100 Census Tract 811, Arapahoe County, Colorado 3895
## 48 08005081200 Census Tract 812, Arapahoe County, Colorado 598
## 49 08005081300 Census Tract 813, Arapahoe County, Colorado 3569
## 50 08005081400 Census Tract 814, Arapahoe County, Colorado 2323
## 51 08005081500 Census Tract 815, Arapahoe County, Colorado 3189
## 52 08005081600 Census Tract 816, Arapahoe County, Colorado 2084
## 53 08005081700 Census Tract 817, Arapahoe County, Colorado 389
## 54 08005081800 Census Tract 818, Arapahoe County, Colorado 5590
## 55 08005081900 Census Tract 819, Arapahoe County, Colorado 3641
## 56 08005082000 Census Tract 820, Arapahoe County, Colorado 3252
## 57 08005082100 Census Tract 821, Arapahoe County, Colorado 2796
## 58 08005082200 Census Tract 822, Arapahoe County, Colorado 2216
## 59 08005082300 Census Tract 823, Arapahoe County, Colorado 1511
## 60 08005082400 Census Tract 824, Arapahoe County, Colorado 2295
## 61 08005082500 Census Tract 825, Arapahoe County, Colorado 225
## 62 08005082600 Census Tract 826, Arapahoe County, Colorado 2786
## 63 08005082700 Census Tract 827, Arapahoe County, Colorado 698
## 64 08005082800 Census Tract 828, Arapahoe County, Colorado 1604
## 65 08005082900 Census Tract 829, Arapahoe County, Colorado 3011
## 66 08005083000 Census Tract 830, Arapahoe County, Colorado 211
## 67 08005083100 Census Tract 831, Arapahoe County, Colorado 1409
## 68 08005083200 Census Tract 832, Arapahoe County, Colorado 146
## 69 08005083300 Census Tract 833, Arapahoe County, Colorado 843
## 70 08005083400 Census Tract 834, Arapahoe County, Colorado 455
## 71 08005083500 Census Tract 835, Arapahoe County, Colorado 703
## 72 08005083600 Census Tract 836, Arapahoe County, Colorado 6238
## 73 08005083700 Census Tract 837, Arapahoe County, Colorado 134
## 74 08005083800 Census Tract 838, Arapahoe County, Colorado 2676
## 75 08005083900 Census Tract 839, Arapahoe County, Colorado 3919
## 76 08005084000 Census Tract 840, Arapahoe County, Colorado 150
## 77 08005084100 Census Tract 841, Arapahoe County, Colorado 1432
## 78 08005084200 Census Tract 842, Arapahoe County, Colorado 627
## 79 08005084300 Census Tract 843, Arapahoe County, Colorado 334
## 80 08005084400 Census Tract 844, Arapahoe County, Colorado 154
## 81 08005084500 Census Tract 845, Arapahoe County, Colorado 262
## 82 08005084600 Census Tract 846, Arapahoe County, Colorado 1375
## 83 08005084700 Census Tract 847, Arapahoe County, Colorado 1904
## 84 08005084800 Census Tract 848, Arapahoe County, Colorado 30
## 85 08005084900 Census Tract 849, Arapahoe County, Colorado 90
## 86 08005085000 Census Tract 850, Arapahoe County, Colorado 114
## 87 08005085100 Census Tract 851, Arapahoe County, Colorado 7
## 88 08005085200 Census Tract 852, Arapahoe County, Colorado 86
## 89 08005085300 Census Tract 853, Arapahoe County, Colorado 453
## 90 08005085400 Census Tract 854, Arapahoe County, Colorado 8
## 91 08005085500 Census Tract 855, Arapahoe County, Colorado 17
## 92 08005085600 Census Tract 856, Arapahoe County, Colorado 6
## 93 08005085700 Census Tract 857, Arapahoe County, Colorado 671
## 94 08005085800 Census Tract 858, Arapahoe County, Colorado 512
## 95 08005085900 Census Tract 859, Arapahoe County, Colorado 205
## 96 08005086000 Census Tract 860, Arapahoe County, Colorado 29
## 97 08005086100 Census Tract 861, Arapahoe County, Colorado 12
## 98 08005086300 Census Tract 863, Arapahoe County, Colorado 191
## 99 08005086400 Census Tract 864, Arapahoe County, Colorado 21
## 100 08005086500 Census Tract 865, Arapahoe County, Colorado 1074
## 101 08005086600 Census Tract 866, Arapahoe County, Colorado 426
## 102 08005086700 Census Tract 867, Arapahoe County, Colorado 174
## 103 08005086800 Census Tract 868, Arapahoe County, Colorado 23
## 104 08005086900 Census Tract 869, Arapahoe County, Colorado 21
## 105 08005087000 Census Tract 870, Arapahoe County, Colorado 900
## 106 08005087100 Census Tract 871, Arapahoe County, Colorado 382
## 107 08005087200 Census Tract 872, Arapahoe County, Colorado 4
## 108 08005087300 Census Tract 873, Arapahoe County, Colorado 1
## 109 08013012607 Census Tract 126.07, Boulder County, Colorado 1
## 110 08031001701 Census Tract 17.01, Denver County, Colorado 3
## 111 08031002403 Census Tract 24.03, Denver County, Colorado 1
## 112 08031002601 Census Tract 26.01, Denver County, Colorado 1
## 113 08031002703 Census Tract 27.03, Denver County, Colorado 3
## 114 08031002801 Census Tract 28.01, Denver County, Colorado 3
## 115 08031002803 Census Tract 28.03, Denver County, Colorado 2
## 116 08031002902 Census Tract 29.02, Denver County, Colorado 1
## 117 08031003003 Census Tract 30.03, Denver County, Colorado 6
## 118 08031003004 Census Tract 30.04, Denver County, Colorado 3
## 119 08031003102 Census Tract 31.02, Denver County, Colorado 3
## 120 08031003201 Census Tract 32.01, Denver County, Colorado 9
## 121 08031003202 Census Tract 32.02, Denver County, Colorado 3
## 122 08031003401 Census Tract 34.01, Denver County, Colorado 8
## 123 08031003402 Census Tract 34.02, Denver County, Colorado 1
## 124 08031004102 Census Tract 41.02, Denver County, Colorado 2
## 125 08031004106 Census Tract 41.06, Denver County, Colorado 195
## 126 08031004107 Census Tract 41.07, Denver County, Colorado 26
## 127 08031004301 Census Tract 43.01, Denver County, Colorado 2
## 128 08031004403 Census Tract 44.03, Denver County, Colorado 35
## 129 08031004404 Census Tract 44.04, Denver County, Colorado 15
## 130 08031004405 Census Tract 44.05, Denver County, Colorado 63
## 131 08031006804 Census Tract 68.04, Denver County, Colorado 1
## 132 08031006811 Census Tract 68.11, Denver County, Colorado 1
## 133 08031006813 Census Tract 68.13, Denver County, Colorado 30
## 134 08031006814 Census Tract 68.14, Denver County, Colorado 6
## 135 08031007006 Census Tract 70.06, Denver County, Colorado 164
## 136 08031007037 Census Tract 70.37, Denver County, Colorado 613
## 137 08031007088 Census Tract 70.88, Denver County, Colorado 1
## 138 08031007089 Census Tract 70.89, Denver County, Colorado 3
## 139 08031008304 Census Tract 83.04, Denver County, Colorado 2
## 140 08031008312 Census Tract 83.12, Denver County, Colorado 15
## 141 08031008388 Census Tract 83.88, Denver County, Colorado 4
## 142 08031008389 Census Tract 83.89, Denver County, Colorado 4
## 143 08031008390 Census Tract 83.90, Denver County, Colorado 4
## 144 08031008391 Census Tract 83.91, Denver County, Colorado 1
## 145 08031980000 Census Tract 9800, Denver County, Colorado 10
## 146 08035013901 Census Tract 139.01, Douglas County, Colorado 381
## 147 08035013904 Census Tract 139.04, Douglas County, Colorado 1
## 148 08035014007 Census Tract 140.07, Douglas County, Colorado 1
##
## $zcta
## GEOID NAME traffic_stops
## 1 0880010 ZCTA5 80010, Colorado 20176
## 2 0880011 ZCTA5 80011, Colorado 36835
## 3 0880012 ZCTA5 80012, Colorado 22681
## 4 0880013 ZCTA5 80013, Colorado 12558
## 5 0880014 ZCTA5 80014, Colorado 16634
## 6 0880015 ZCTA5 80015, Colorado 9762
## 7 0880016 ZCTA5 80016, Colorado 4154
## 8 0880017 ZCTA5 80017, Colorado 15986
## 9 0880018 ZCTA5 80018, Colorado 264
## 10 0880019 ZCTA5 80019, Colorado 175
## 11 0880022 ZCTA5 80022, Colorado 1
## 12 0880045 ZCTA5 80045, Colorado 925
## 13 0880102 ZCTA5 80102, Colorado 1
## 14 0880111 ZCTA5 80111, Colorado 4
## 15 0880112 ZCTA5 80112, Colorado 123
## 16 0880113 ZCTA5 80113, Colorado 2
## 17 0880121 ZCTA5 80121, Colorado 1
## 18 0880122 ZCTA5 80122, Colorado 1
## 19 0880137 ZCTA5 80137, Colorado 2
## 20 0880138 ZCTA5 80138, Colorado 16
## 21 0880202 ZCTA5 80202, Colorado 3
## 22 0880203 ZCTA5 80203, Colorado 2
## 23 0880205 ZCTA5 80205, Colorado 1
## 24 0880206 ZCTA5 80206, Colorado 3
## 25 0880207 ZCTA5 80207, Colorado 2
## 26 0880209 ZCTA5 80209, Colorado 11
## 27 0880210 ZCTA5 80210, Colorado 10
## 28 0880218 ZCTA5 80218, Colorado 17
## 29 0880220 ZCTA5 80220, Colorado 82
## 30 0880230 ZCTA5 80230, Colorado 61
## 31 0880231 ZCTA5 80231, Colorado 44
## 32 0880237 ZCTA5 80237, Colorado 1
## 33 0880238 ZCTA5 80238, Colorado 189
## 34 0880239 ZCTA5 80239, Colorado 29
## 35 0880247 ZCTA5 80247, Colorado 721
## 36 0880249 ZCTA5 80249, Colorado 19
## 37 0880303 ZCTA5 80303, Colorado 1
## 38 <NA> <NA> 25
geography_stop_list[["county"]]
## GEOID NAME traffic_stops
## 1 08001 Adams County, Colorado 22489
## 2 08005 Arapahoe County, Colorado 117391
## 3 08013 Boulder County, Colorado 1
## 4 08031 Denver County, Colorado 1244
## 5 08035 Douglas County, Colorado 383
geography_stop_list[[1]]
## GEOID NAME traffic_stops
## 1 08001 Adams County, Colorado 22489
## 2 08005 Arapahoe County, Colorado 117391
## 3 08013 Boulder County, Colorado 1
## 4 08031 Denver County, Colorado 1244
## 5 08035 Douglas County, Colorado 383
output_csv<-function(files, name){
filename<-paste0(name, ".csv")
write_csv(files, filename)
}
# Test function
output_csv(county_subdivision_stops, "county_subdivision")
walk2(geography_stop_list, desired_geography_inputs, .f=output_csv)
country_boundaries<-ne_countries(scale="medium", returnclass="sf") %>%
filter(iso_a3 !="ATA")
tm_shape(country_boundaries)+
tm_polygons()
trade_gdp_2010_2018<-WDI(country="all", # specifies we want data for all countries available
indicator="NE.TRD.GNFS.ZS", # specifies code for desired indicator
start=2010, # Start year for data series
end=2018, # End year for data series
extra=T) # returns
trade_gdp_2015<-
trade_gdp_2010_2018 %>% # Establishes object to be modified
filter(year=="2015") %>% # Subsets observations where the "year" variable equals "2015"
rename(trade_gdp_2015=NE.TRD.GNFS.ZS)
trade_2015_spatial<-full_join(country_boundaries, trade_gdp_2015,
by=c("iso_a3"="iso3c"))
tm_shape(trade_2015_spatial)+
tm_polygons(col="trade_gdp_2015",
n=7,
style="quantile",
palette="YlOrBr",
title="Trade as a % of GDP,\n2015",
textNA="No Data")+
tm_layout(legend.outside=T,
legend.outside.position = "bottom",
main.title="Crossnational Variation in Commercial Integration, 2015",
main.title.size=1,
main.title.position="center",
inner.margins=c(0.06,0.10,0.10,0.08), # Sets margins to create whitespace
frame=FALSE,
attr.outside = TRUE)+ # Places credits section outside map
tm_credits("Map Author: Aditya Ranganath\nData Source: World Bank\nDevelopment Indicators (WDI)", position=c(0.78,0), size=0.38) # Specifies content, position, size of credits
## Warning: The shape trade_2015_spatial contains empty units.
wdi_map_maker<-function(wdi_variable_code, start_year, end_year,
legend.title, main_map_title){
country_boundaries<-ne_countries(scale="medium", returnclass="sf") %>%
filter(iso_a3 !="ATA")
wdi_extract<-WDI(country="all",
indicator=wdi_variable_code,
start=start_year,
end=end_year,
extra=T)
spatial_object_tomap<-inner_join(country_boundaries, wdi_extract,
by=c("iso_a3"="iso3c"))
final_map<-tm_shape(spatial_object_tomap)+
tm_polygons(col=wdi_variable_code,
n=7,
style="quantile",
palette="YlOrBr",
title=legend.title,
textNA="No Data")+
tm_layout(legend.outside=T,
legend.outside.position = "bottom",
main.title=main_map_title,
main.title.size=1,
main.title.position="center",
inner.margins=c(0.06,0.10,0.10,0.08), # Sets margins to create whitespace
frame=FALSE,
attr.outside = TRUE)+ # Places credits section outside map
tm_credits("Map Author: Aditya Ranganath\nData Source: World Bank\nDevelopment Indicators (WDI)", position=c(0.78,0), size=0.38) # Specifies content, position, size of credits
return(final_map)
}
wdi_map_maker(wdi_variable_code="BG.GSR.NFSV.GD.ZS", start_year=2017, end_year=2017,
legend.title="Services Trade (% of GDP)", main_map_title="Service Market Integration, 2017")
wdi_map_maker(wdi_variable_code="GC.TAX.TOTL.GD.ZS", start_year=2017, end_year=2017,
legend.title="Taxes (% of GDP)", main_map_title="Government Taxes as a Share of GDP, 2017")
input_list<-list(wdi_variable_code=c("BG.GSR.NFSV.GD.ZS", "GC.TAX.TOTL.GD.ZS"),
start_year=c(2017, 2017),
end_year=c(2017, 2017),
legend.title=c("Services Trade (% of GDP)", "Taxes (%GDP)"),
main_map_title=c("Service Market Integration, 2017", "Government Taxes as a Share of GDP, 2017"))
map_list<-pmap(input_list, wdi_map_maker)
map_list
## [[1]]
##
## [[2]]
map_list[[1]]
map_list[[2]]
names(map_list)<-input_list$wdi_variable_code
map_list
## $BG.GSR.NFSV.GD.ZS
##
## $GC.TAX.TOTL.GD.ZS
map_list[["BG.GSR.NFSV.GD.ZS"]]
map_list[["GC.TAX.TOTL.GD.ZS"]]
map_list %>% pluck("GC.TAX.TOTL.GD.ZS")
# Read in NYC 2019 Raster
nyc_pop_2019<-raster("nyc_2019_pop.tif")
# Read in NYC subway stop data
nyc_subway_stops<-st_read("stops_nyc_subway_may2019.shp")
## Reading layer `stops_nyc_subway_may2019' from data source `/Users/adra7980/Documents/git_repositories/ARSC5040_GIS/class_notes/class4/scripting_data/stops_nyc_subway_may2019.shp' using driver `ESRI Shapefile'
## Simple feature collection with 493 features and 9 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: 914189.9 ymin: 126191.2 xmax: 1052169 ymax: 268346.1
## projected CRS: NAD83 / New York Long Island (ftUS)
# Read in NYC borough data
nyc_boroughs<-st_read("nyu_2451_34490.shp")
## Reading layer `nyu_2451_34490' from data source `/Users/adra7980/Documents/git_repositories/ARSC5040_GIS/class_notes/class4/scripting_data/nyu_2451_34490.shp' using driver `ESRI Shapefile'
## Simple feature collection with 5 features and 4 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 913090.7 ymin: 120053.5 xmax: 1067310 ymax: 272752.9
## projected CRS: NAD83 / New York Long Island (ftUS)
# Create 500m subway buffers
nyc_subway_500m_buffer<-st_buffer(nyc_subway_stops, 1640.42)
# Dissolve buffers
nyc_subway_500m_buffer_dissolved<-nyc_subway_500m_buffer %>%
group_by() %>%
summarise()
# Transform CRS of "nyc_subway_500m_buffer_dissolved" to match "nyc_pop_2019"
nyc_subway_500m_buffer_dissolved_4326<-nyc_subway_500m_buffer_dissolved %>%
st_transform(4326)
# Calculate population in buffer zone ("nyc_subway_500m_buffer_dissolved_4326") based on "nyc_2019_population" raster
nyc_pop_within_buffer<-exact_extract(nyc_pop_2019, nyc_subway_500m_buffer_dissolved_4326, fun="sum")
# Extract total NYC population
nyc_borough_4326<-nyc_boroughs %>% st_transform(4326)
nyc_pop<-sum(exact_extract(nyc_pop_2019, nyc_borough_4326, fun="sum" ))
##
|
| | 0%
|
|============== | 20%
|
|============================ | 40%
|
|========================================== | 60%
|
|======================================================== | 80%
|
|======================================================================| 100%
# Percentage Inside Buffer
pct_inside<-(nyc_pop_within_buffer/nyc_pop)*100
# Calculate Percentage Outside Buffer
pct_outside_buffer<-100-pct_inside
# Print "pct_outside_buffer"
pct_outside_buffer
## [1] 45.07731